package com.base.api.sys.impl;

import com.alibaba.fastjson.JSONObject;
import com.base.api.sys.ISysMenuService;
import com.base.dao.sys.MenuMapper;
import com.base.utils.ResultUtil;
import com.base.utils.TreeUtil;
import com.base.vo.sys.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@CacheConfig(cacheNames = "sys")
@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String save(MenuVo sysMenu) {
        try {
            if(!"".equals(sysMenu.getIcon())&&sysMenu.getIcon()!=null){
                sysMenu.setIcon("fa "+sysMenu.getIcon());
            }
            stringRedisTemplate.delete("sys::sys_AllSysMenu");
            stringRedisTemplate.delete("sys::sys_AllSysMenuJson");
            // 负数为新增，正数为修改
            if (sysMenu.getId() < 0) {
                sysMenu.setId(menuMapper.getSquence());
                menuMapper.insert(sysMenu);
                return ResultUtil.success();
            } else {
                sysMenu.setUpdateAt(new Date());
                menuMapper.updateByPrimaryKeySelective(sysMenu);
                return ResultUtil.success();
            }
        } catch (Exception e) {
            return ResultUtil.error(400, e.getMessage());
        }
    }


    @Override
    public String delete(Integer id) {
        menuMapper.deleteByPrimaryKey(id);
        stringRedisTemplate.delete("sys::sys_AllSysMenu");
        stringRedisTemplate.delete("sys::sys_AllSysMenuJson");
        return ResultUtil.success();
    }

    /**
     * 返回菜单数据
     *
     * @return
     */
    @Cacheable(key = "'sys_AllSysMenu'")
    @Override
    public Map<String, Object> findSysMenu() {
            Map<String,Object> map = new HashMap<>();
            List<MenuVo> allMenu = menuMapper.findAllMenu();
            map.put("count",allMenu.size());
            map.put("data",allMenu);
            map.put("code",0);
            map.put("msg","");
            return map;
    }

    @Cacheable(key = "'sys_AllSysMenuJson'")
    @Override
    public Map<String, Object> findTree() {
        Map<String, Object> map = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);
        List<MenuVo> menuInfo = menuMapper.findAllMenu();
        map.put("menuInfo", TreeUtil.toTree(menuInfo, 0));
        home.put("title","首页");
        home.put("href","/html/page/welcome-1");//控制器路由,自行定义
        logo.put("title","实施工具");
        logo.put("image","/images/logo.png");//静态资源文件路径,可使用默认的logo.png
        map.put("homeInfo", home);
        map.put("logoInfo", logo);
        return map;
    }

    /**
     * 清理缓存服务器
     *
     * @return
     */
    @Override
    public Map<String, Object> clearCache() {
        Set<String> keys = stringRedisTemplate.keys("*");
        if(!keys.isEmpty()){
            stringRedisTemplate.delete(keys);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code",1);
        map.put("msg","服务端清理缓存成功,成功清理 " + keys.size() + " 个缓存!");
        return map;
    }
}
